// SPDX-License-Identifier: GPL-2.0
/*
 * PFNMAPPED MEMORY
 *
 *  => CMDLINE: "memmap=4K$0x10000000"
 *
 * (C) 2024.05.01 BuddyZhang1 <buddy.zhang@aliyun.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>

#define MAP_VADDR	0x600000000
#define MAP_SIZE	0x1000
#define RSVDMEM_BASE	0x10000000

int main()
{
	void *mem;
	int fd;

	/* OPEN SPECIAL FILE */
	fd = open("/dev/mem", O_RDWR);
	if (fd < 0) {
		printf("ERROR: OPEN FAILED.\n");
		return -1;
	}

	/* ALLOC MEMORY */
	mem = mmap((void *)MAP_VADDR, MAP_SIZE,
		   PROT_READ | PROT_WRITE,
		   MAP_SHARED,
		   fd,
		   RSVDMEM_BASE);
	if (mem == MAP_FAILED) {
		printf("ERROR: ALLOC MEMORY FAILED.\n");
		close(fd);
		return -1;
	}

	/* ACCESS */
	*(char *)mem = 'B';
	printf("PFNMAPPED MEMORY: %#lx %c\n",
			(unsigned long)mem, *(char *)mem);

	/* RECLAIM */
	munmap(mem, MAP_SIZE);
	close(fd);

	return 0;
}
